package com.kunminx.viabus.ui;

import com.kunminx.viabus.ResourceTable;
import com.kunminx.viabus.ui.adapter.ListProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import com.kunminx.core.bus.Result;
import com.kunminx.architecture.base.BaseBusSlice;
import com.kunminx.viabus.business.bus.NoteBus;
import com.kunminx.viabus.business.constant.NoteResultCode;
import com.kunminx.viabus.repertory.bean.NoteBean;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author KunMinX
 * Create at 2018/8/21
 */
public class TodoListSlice extends BaseBusSlice {

    private ListContainer listContainer;
    private ListProvider mAdapter;
    private AbilitySlice slice = this;
    private ArrayList<NoteBean> mList = new ArrayList<>();

    public static TodoListSlice newInstance() {
        TodoListSlice slice = new TodoListSlice();
        return slice;
    }

    /**
     * onStart
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // 注册监听
        NoteBus.registerResponseObserver(this);
        // 查询 表
        NoteBus.note().queryList();

        // 帮页面
        setUIContent(ResourceTable.Layout_ability_main);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        // 创建 listcontainer
        initViews();

        // 新增
        Button btnDataSelect = (Button) findComponentById(ResourceTable.Id_fab);
        if (btnDataSelect != null) {
            // 为按钮设置点击回调
            btnDataSelect.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    // 新增
                    ClickProxy.newNote();
                }
            });
        }
    }


    // 初始化列表元素
    private void initViews() {
        mAdapter = new ListProvider(mList, slice);
        listContainer.setItemProvider(mAdapter);

        // 下面相当于 NoteListAdapter 的onBindItem 列表的绑定事件
        // 点击 更新
        listContainer.setItemClickedListener((container, component, position, id) -> {
            NoteBean item = (NoteBean) listContainer.getItemProvider().getItem(position);
            item.setTitle(UUID.randomUUID().toString());
            mAdapter.notifyDataSetItemChanged(position);
            NoteBus.note().update(item);
        });
        //  绑定删除 长按
        listContainer.setItemLongClickedListener((container, component, position, id) -> {
            NoteBean item = (NoteBean) listContainer.getItemProvider().getItem(position);
            mList.remove(position);
            mAdapter.notifyDataSetItemRemoved(position);
            NoteBus.note().delete(item);
            return false;
        });
    }

    /**
     * setList 赋值列表
     *
     * @param list
     */
    public void setList(List<NoteBean> list) {
        if (list != null) {
            mList.clear();
            mList.addAll(list);
        }
    }

    /**
     * onStop
     */
    @Override
    public void onStop() {
        super.onStop();
        NoteBus.unregisterResponseObserver(this);
    }

    /**
     * 回调 监听数据变化后
     */
    @Override
    public void onResultHandle(Result testResult) {
        String resultCode = (String) testResult.getResultCode();
        switch (resultCode) {
            case NoteResultCode.QUERY_LIST:
                if (testResult.getResultObject() != null) {
                    setList((List<NoteBean>) testResult.getResultObject());
                    mAdapter.notifyDataSetItemRangeChanged(0, mList.size());
                }
                break;
            case NoteResultCode.INSERTED:
                if (testResult.getResultObject() != null) {
                    mList.add(0, (NoteBean) testResult.getResultObject());
                    mAdapter.notifyDataSetItemInserted(0);
                }
                break;
            case NoteResultCode.FAILURE:
                new ToastDialog(getContext())
                        .setText(testResult.getResultObject().toString())
                        .show();
                break;
            case NoteResultCode.CANCELED:
                new ToastDialog(getContext())
                        .setText(testResult.getResultObject().toString())
                        .show();
                break;
            default:
        }
    }

    // 点击新增
    public static class ClickProxy {
        public static void newNote() {
            // 上面改为
            NoteBean bean = new NoteBean();
            bean.setTitle(UUID.randomUUID().toString());
            bean.setDate(String.valueOf(System.currentTimeMillis()));
            bean.setImgUrl("https://upload-images.jianshu.io/upload_images/57036-fb9653da874d2447.jpg");
            NoteBus.note().insert(bean);
        }
    }

}
